package com.aaa.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/8/15 10:09
 * @description：我自己的sqlsesion工具类
 * @modified By：
 * @version:
 */
public class MyMybatisSqlSessionTool {
    static  SqlSessionFactory sqlSessionFactory;
    static {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            //去读资源文件mybatis-config.xml，转换成流
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //SqlSessionFactoryBuilder的build方法依赖上面的配置文件
         sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    public static SqlSession getSqlSession(){
        /**
         * @create by: Teacher陈（86521760@qq.com）
         * @description: 通用的获取sqlSession的方法
         * @create time: 2022/8/15 10:12
         * @param
         * @return org.apache.ibatis.session.SqlSession
         */
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }
}
